package com.ebt.m.commons.model.http;


import android.text.TextUtils;
import android.util.Pair;

import com.ebt.m.commons.BaseApplication;
import com.ebt.m.commons.model.events.TokenEvent;
import com.ebt.m.commons.utils.ToastUtil;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;

import org.greenrobot.eventbus.EventBus;

import retrofit2.HttpException;


/**
 * Created by wanglu on 2016/12/19.
 */

public class Errors {

    public static final String CODE_PASSWORD_ERROR = "100001";
    public static final String CODE_USER_NOT_FOUND = "100002";
    public static final String CODE_USERNAME_ERROR = "100003";
    public static final String CODE_PASSWORD_TIMES = "100004";
    public static final String CODE_LOGIN_TIMES = "100005";
    public static final String CODE_SINGN_TIMEOUT = "100102";
    public static final String CODE_TOKEN_INVALID = "100202";
    public static final String CODE_VERIFICATION_CODE_INVALIDED = "100018";
    public static final String CODE_NOT_AUTHENTICATED = "100301";
    public static final String CODE_NOT_BINDED_WITH_ERP = "100302";
    public static final String CODE_VALIDATECODE_ERROR = "100014";
    public static final String CODE_ERROR_NO_PROPOSAL = "100800";

    public static void errorMessage(Throwable throwable, String outMessage) {
        Pair<Boolean, String> handleResult = null;
        if (throwable instanceof ErrorException) {
            handleResult = getErrorMessage(new Gson().fromJson(throwable.getMessage(), ErrorResponse.class));
        } else if (throwable instanceof HttpException) {
            handleResult = (errorResponse((HttpException) throwable));
        }
        if (handleResult == null || !handleResult.first) {
            ToastUtil.show(BaseApplication.getBaseApplication(), TextUtils.isEmpty(outMessage) ? "获取数据失败" : outMessage);
        } else if (!TextUtils.isEmpty(handleResult.second)) {
            ToastUtil.show(BaseApplication.getBaseApplication(), handleResult.second);
        }
    }

    public static Pair<Boolean, String> errorResponse(HttpException throwable) {
        try {
            String errorInfo = throwable.response().errorBody().string();
            JsonElement jsonElement = new JsonParser().parse(errorInfo);
            return getErrorMessage(new Gson().fromJson(jsonElement.getAsJsonObject().get("error"), ErrorResponse.class));
        } catch (Exception e) {
            return null;
        }
    }

    private static Pair<Boolean, String> getErrorMessage(ErrorResponse errorResponse) {
        String message = "";
        boolean isConsumed = true;
        String code = errorResponse.getCode();
        switch (code) {
            case CODE_PASSWORD_ERROR:
            case CODE_USERNAME_ERROR:
                message = "用户名或者密码错误";
                break;
            case CODE_USER_NOT_FOUND:
                message = "用户名不存在";
                break;
            case CODE_PASSWORD_TIMES:
                message = "密码错误次数超过当天限制";
                break;
            case CODE_LOGIN_TIMES:
                message = "登录过于频繁";
                break;
            case CODE_SINGN_TIMEOUT:
                message = "请求超时";
                break;
            case CODE_TOKEN_INVALID:
                message = "";
                EventBus.getDefault().post(new TokenEvent());
                break;
            case CODE_VERIFICATION_CODE_INVALIDED:
                message = "手机验证码无效";
                break;
            case CODE_NOT_AUTHENTICATED:
                message = "未认证合作公司";
                break;
            case CODE_NOT_BINDED_WITH_ERP:
                message = "未跟系统员工进行绑定";
                break;
            case CODE_VALIDATECODE_ERROR:
                message = "验证码发送失败，请稍后重试";
                break;
            case CODE_ERROR_NO_PROPOSAL:
                message = errorResponse.getMessage();
                if (TextUtils.isEmpty(message)) {
                    message = "计划书不存在";
                }
                break;
            default:
                isConsumed = false;
        }
        return new Pair<>(isConsumed, message);
    }

}
